Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Codec Iteration and Checking of HardwareConfigMethodFlags #40

Merged
merged 4 commits into from
Jan 30, 2024
Merged

Enhance Codec Iteration and Checking of HardwareConfigMethodFlags #40

merged 4 commits into from
Jan 30, 2024

Conversation

Cacsjep
Copy link
Contributor

@Cacsjep Cacsjep commented Jan 28, 2024

Hey

During intergration steps of hw decoding I see that some thing would be very handy.

1. Hardware Configuration Flag Check:
A method HasHardwareConfigMethodFlag in the Codec struct allows checking if a codec supports a specific hardware configuration method. This is particularly useful for identifying if a codec is hardware accelerated.

Implementation snippet:

func (c *Codec) HasHardwareConfigMethodFlag(chcmf CodecHardwareConfigMethodFlag) bool {
    var i C.int
    for {
        config := C.avcodec_get_hw_config(c.c, i)
        if config == nil {
            break
        }
        if (CodecHardwareConfig{c: config}.MethodFlags().Has(chcmf)) {
            return true
        }
        i++
    }
    return false
}

2. Codec Iteration Enhancement:
A new function IterateCodecs simplifies the process of iterating over available codecs. This function takes a CodecProcessor and applies it to each codec, facilitating custom processing logic per codec.

Implementation snippet:

func IterateCodecs(processor CodecProcessor) {
    var opq *C.void = nil
    for {
        c := C.av_codec_iterate((*unsafe.Pointer)(unsafe.Pointer(&opq)))
        if c == nil {
            break
        }
        codec := newCodecFromC(c)
        processor(codec)
    }
}

3. Codec Identification:
Added a method ID to the Codec struct to retrieve the codec's ID, aiding in the identification and filtering of codecs.

func (c *Codec) ID() CodecID {
    return CodecID(c.c.id)
}

Use Case
These enhancements allow for efficient identification and selection of codecs based on capabilities and specific codec IDs. This is particularly useful in scenarios where an application needs to support multiple codecs across different operating systems and offers the user the ability to switch between different decoders.

A practical example of using these enhancements is shown below:

var decoders []*astiav.Codec
processor := func(c *astiav.Codec) {
    if c.IsDecoder() && c.HasHardwareConfigMethodFlag(astiav.CodecHardwareConfigMethodFlagHwDeviceCtx) && c.ID() == astiav.CodecIDMjpeg {
        decoders = append(decoders, c)
    }
}
astiav.IterateCodecs(processor)

Br christoph

…support hw

HasHardwareConfigMethodFlag is tested on my local device but does not work in git
Copy link
Owner

@asticode asticode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions ! ❤️

codec.go Outdated Show resolved Hide resolved
codec.go Outdated Show resolved Hide resolved
codec_test.go Show resolved Hide resolved
codec_test.go Outdated Show resolved Hide resolved
Remove unused arg in HardwareConfigs
Add mehtod to get all codecs
Update test
@Cacsjep
Copy link
Contributor Author

Cacsjep commented Jan 30, 2024

Done =)

@asticode
Copy link
Owner

Perfect, I'll merge the PR and make minor adjustments 👍

@asticode asticode merged commit 063a68b into asticode:master Jan 30, 2024
3 checks passed
@Cacsjep Cacsjep deleted the codec_iterator branch January 30, 2024 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants